using System;
using System.Collections.Generic;
using System.Data.SQLite;
using Jonathan.Classes;
using System.Windows.Forms;

namespace Jonathan.Controls
{
   /// <summary>
   /// Start control.
   /// </summary>
   public sealed partial class CtrlStart : UserControl
   {
      private readonly FrmMain m_FormMain;

      /// <summary>
      /// Initializes a new start control.
      /// </summary>
      /// <param name="main">Main form.</param>
      public CtrlStart (FrmMain main)
      {
         InitializeComponent (main);

         this.m_FormMain = main;
         this.Dock = DockStyle.Fill;

         this.m_RandVerseCtrl.FormMain = main;
         this.m_RandVerseCtrl.Font = this.m_FormMain.ProgramData.DefaultFont.ToFont ();
         this.m_DailyDevCtrl.Font = this.m_FormMain.ProgramData.DefaultFont.ToFont ();

         // Load a random vese...
         this.GetRandomVerse ();

         // Load a Daily Devotional...
         this.LoadDaily ();
      }

      private void LoadDaily ()
      {
         string path = "";
         foreach (CjData.IcDbInfo dbI in this.m_FormMain.ProgramData.DailyDatabases)
         {
            if (dbI.Name.Equals (this.m_FormMain.ProgramData.DefaultDaily))
            {
               path = Common.DataFolder + dbI.Path;
               break;
            }
         }

         if (!string.IsNullOrEmpty (path))
         {
            using (SQLiteConnection dbConn = new SQLiteConnection (Common.ConnString (path)))
            {
               dbConn.Open ();
               SQLiteCommand command = dbConn.CreateCommand ();
               command.CommandText = string.Format ("SELECT * FROM book_text WHERE Month='{0}' AND Day={1} AND Time='{2}'",
               GetMonth (DateTime.Today.Month), DateTime.Today.Day, DateTime.Now.Hour < 12 ? "Morning" : "Evening");

               SQLiteDataReader reader = command.ExecuteReader();
               reader.Read ();
               this.m_DailyDevCtrl.SetData (reader.GetString (reader.GetOrdinal ("Title")),
                                            reader.GetString (reader.GetOrdinal ("Text")),
                                            reader.GetString (reader.GetOrdinal ("Reference")),
                                            reader.GetString (reader.GetOrdinal ("Notes")));
            }
         }
      }

      private static string GetMonth (int month)
      {
         switch (month)
         {
            case 1:
               return "January";
            case 2:
               return "February";
            case 3:
               return "March";
            case 4:
               return "April";
            case 5:
               return "May";
            case 6:
               return "June";
            case 7:
               return "July";
            case 8:
               return "August";
            case 9:
               return "September";
            case 10:
               return "October";
            case 11:
               return "November";
            default:
               return "December";
         }
      }
      /// <summary>
      /// A Method to update the Font...
      /// </summary>
      public void UpdateFont ()
      {
         this.m_DailyDevCtrl.Font = this.m_FormMain.ProgramData.DefaultFont.ToFont ();
         this.m_RandVerseCtrl.Font = this.m_FormMain.ProgramData.DefaultFont.ToFont ();
      }

      #region - Random Verse -

      /// <summary>
      /// Load a random verse...
      /// </summary>
      private void GetRandomVerse ()
      {
         List<CjData.IcDbInfo> sources = new List<CjData.IcDbInfo> ();
         sources.AddRange (this.m_FormMain.ProgramData.BibleDatabases);
         sources.AddRange (this.m_FormMain.ProgramData.OtherDatabases);

         // Get Bible...
         Random rand = new Random (DateTime.Now.Millisecond);
         int randTemp = rand.Next (sources.Count);
         string bible = sources[randTemp].Name;
         string bibleFile = sources[randTemp].Path;

         using (SQLiteConnection dbConn = new SQLiteConnection (Common.ConnString(Common.DataFolder + bibleFile)))
         {
            dbConn.Open ();
            SQLiteCommand command = dbConn.CreateCommand ();
            command.CommandText = "SELECT * FROM book_text ORDER BY RANDOM() LIMIT 1;";
            SQLiteDataReader reader = command.ExecuteReader ();
            reader.Read ();

            string book = reader.GetString (reader.GetOrdinal ("Book"));
            int chap = reader.GetInt32(reader.GetOrdinal ("Chapter"));
            int verse = reader.GetInt32 (reader.GetOrdinal ("Verse"));
            string text = reader.GetString (reader.GetOrdinal ("Text"));
            string loc = book + " " + chap + ":" + verse;
            this.m_RandVerseCtrl.SetVerse (text, loc, bible, book, chap.ToString(), verse.ToString());
         }
      }

      #endregion
   }
}
